package cn.autowired.exam.controller;

import cn.autowired.exam.base.CommonResponse;
import cn.autowired.exam.base.NickName;
import cn.autowired.exam.utils.ZXingCodeUtils;
import org.hamcrest.core.Is;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import cn.autowired.exam.controller.AbstractController;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.mapper.EntityWrapper;

import cn.autowired.exam.service.MsgService;

import cn.autowired.exam.entity.Msg;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 *
 *   @description : Msg 控制器
 *   ---------------------------------
 */
@RestController
@RequestMapping("/msg")
public class MsgController extends AbstractController {
private final Logger logger=LoggerFactory.getLogger(MsgController.class);

    @Autowired
    public MsgService msgService;

    /**
     * 发送消息
     * @param message
     * @param teacherId
     * @param studentNo
     * @param sex
     * @return
     */
    @RequestMapping("/send")
    public ResponseEntity sendMsg(String message,Integer teacherId,String studentNo,Integer sex){
        Msg msg = new Msg();
        msg.setMessage(message);
        msg.setStudentNo(studentNo);
        msg.setNickname(sex==null?"小白":sex==1?NickName.boyName():NickName.girlName());
        msg.setTeacherId(teacherId);
        if (studentNo==""||teacherId==null||teacherId==0){
            return ResponseEntity.ok(CommonResponse.error());
        }
        boolean flag = msg.insert();
        return ResponseEntity.ok(flag?CommonResponse.ok():CommonResponse.error());
    }

    /**
     * 用于预约和回复
     * @param message
     * @param teacherId
     * @param studentNo
     * @param application
     * @return
     */
    public ResponseEntity sendMsg(String message,Integer teacherId,String studentNo,boolean application){
        Msg msg = new Msg();
        msg.setMessage(message);
        //如果application为true 则此消息为 申请预约者发送 , 反之为老师回复
        msg.setNickname(application?"student":"teacher");
        msg.setTeacherId(teacherId);
        msg.setStudentNo(studentNo);

        boolean flag = msg.insert();
        return ResponseEntity.ok(flag?CommonResponse.ok():CommonResponse.error());
    }

    /**
     * 回复消息
     * @param id
     * @param res
     * @return
     */
    @RequestMapping("/res")
    public ResponseEntity responseMsg(Integer id,String res){
        Msg msg = msgService.selectById(id);
        msg.setRes(res);
        boolean flag = msg.updateById();
        return ResponseEntity.ok(flag?CommonResponse.ok():CommonResponse.error());
    }

    /**
     * 根据老师ID获取所有消息
     * @param teacherId
     * @return
     */
    @GetMapping("/getMsgListByTeacherId")
    public ResponseEntity getMsgListByTeacherId(Integer teacherId){
        List<Msg> msgs = msgService.selectList(new EntityWrapper<Msg>()
                .eq(Msg.Field.teacherId, teacherId));
        return ResponseEntity.ok(CommonResponse.ok(msgs));
    }
    /**
     * 根据老师ID获取所有未评论消息
     * @param teacherId
     * @return
     */
    @PostMapping("/getMsgListByTeacherId")
    public ResponseEntity getMsgListByTeacherIdUnResponse(Integer teacherId){
        List<Msg> msgs = msgService.selectList(new EntityWrapper<Msg>()
                .eq(Msg.Field.teacherId, teacherId)
                .isNull(Msg.Field.res));
        return ResponseEntity.ok(CommonResponse.ok(msgs));
    }

    /**
     * 根据学号获取所有消息
     * @param studentNo
     * @return
     */
    @GetMapping("/getMsgListByStudentNo")
    public ResponseEntity getMsgListByStudentNo(String studentNo){
        List<Msg> msgs = msgService.selectList(new EntityWrapper<Msg>()
                .eq(Msg.Field.studentNo, studentNo));
        return ResponseEntity.ok(CommonResponse.ok(msgs));
    }

    /**
     * 根据学号获取所有未回复消息
     * @param studentNo
     * @return
     */
    @PostMapping("/getMsgListByStudentNo")
    public ResponseEntity getMsgListByStudentNoUnResponse(String studentNo){
        List<Msg> msgs = msgService.selectList(new EntityWrapper<Msg>()
                .eq(Msg.Field.studentNo, studentNo)
        .isNull(Msg.Field.res));
        return ResponseEntity.ok(CommonResponse.ok(msgs));
    }

    /**
     * 根据消息片段查询消息列表
     * @param message
     * @return
     */
    @RequestMapping("/getMsgListByMessage")
    public ResponseEntity getMsgListByMessage(String message){
        List<Msg> msgs = msgService.selectList(new EntityWrapper<Msg>().like(Msg.Field.message, message));
        return ResponseEntity.ok(CommonResponse.ok(msgs));
    }

    /**
     * 查询在线预约申请信息
     * @return
     */
    @RequestMapping("/getApplicationByStudentNo")
    public ResponseEntity getApplicationList(String studentNo){
        List<Msg> msgs = msgService.selectList(new EntityWrapper<Msg>().eq(Msg.Field.nickname, "student")
        .eq(Msg.Field.studentNo,studentNo));
        return ResponseEntity.ok(CommonResponse.ok(msgs));
    }
    /**
     * 查询在线预约申请信息(老师)
     * @return
     */
    @RequestMapping("/getApplicationInfoByTeacherId")
    public ResponseEntity getApplicationList(Integer teacherId){
        List<Msg> msgs = msgService.selectList(new EntityWrapper<Msg>().eq(Msg.Field.nickname, "student")
                .eq(Msg.Field.teacherId,teacherId));
        return ResponseEntity.ok(CommonResponse.ok(msgs));
    }
    /**
     * 查询在线预约回复信息(学生)
     * @return
     */
    @RequestMapping("/getApplicationResByTeacherId")
    public ResponseEntity getApplicationListRes(Integer teacherId){
        List<Msg> msgs = msgService.selectList(new EntityWrapper<Msg>().eq(Msg.Field.nickname, "teacher")
                .eq(Msg.Field.teacherId,teacherId));
        return ResponseEntity.ok(CommonResponse.ok(msgs));
    }
    /**
     * 查询在线预约回复信息
     * @return
     */
    @RequestMapping("/getApplicationResByStudentNo")
    public ResponseEntity getApplicationListRes(String studentNo){
        List<Msg> msgs = msgService.selectList(new EntityWrapper<Msg>().eq(Msg.Field.nickname, "teacher")
                .eq(Msg.Field.studentNo,studentNo));
        return ResponseEntity.ok(CommonResponse.ok(msgs));
    }

    @RequestMapping("/deleteMsgById")
    public ResponseEntity deleteMsgById(Integer id){
        msgService.deleteById(id);
        return ResponseEntity.ok(CommonResponse.ok());
    }

    /**
     * 获取二维码 扫码进入在线咨询页面
     * @param studentNo
     * @return
     */
    @RequestMapping("/getQRCode")
    public ResponseEntity getQRCode(String studentNo){
       return ResponseEntity.ok(CommonResponse.ok(ZXingCodeUtils.createQRCode(null,"http://wwww.blinkq.cn/page/mobile/msg.html?studentNo="+studentNo,"",200,200,"在线咨询")));
    }


    /**
     * 获取二维码 扫码进入在线咨询页面(本地版本)
     * @param studentNo
     * @return
     */
    @RequestMapping("/getQRCodeOffline")
    public ResponseEntity getQRCodeOffline(String studentNo){
        return ResponseEntity.ok(CommonResponse.ok(ZXingCodeUtils.createQRCode(null,"http://wwww.blinkq.cn/page/mobile/msg.html?studentNo="+studentNo,"",200,200,"在线咨询")));
    }
}